﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using JYStockAnalyser.Model;
using JYStockAnalyser.ClientLib;

namespace JYStockAnalyser.Manager
{
    public partial class UserEditor : UserControl
    {
        User m_currentUser;

        public UserEditor()
        {
            InitializeComponent();
        }

        public User GetUser()
        {
            if (m_currentUser == null)
                m_currentUser = new User();
            m_currentUser.Name = this.txtName.Text.Trim();
            m_currentUser.UserName = this.txtUserName.Text.Trim();
            m_currentUser.Password = this.txtPassword.Text.Trim();

            if (this.rbtnMan.Checked) m_currentUser.Sex = "男";
            if (this.rbtnWomen.Checked) m_currentUser.Sex = "女";
            
            m_currentUser.Work = this.txtWork.Text.Trim();
            m_currentUser.ContractNO = this.txtContractNO.Text.Trim();
            m_currentUser.Email = this.txtEmail.Text.Trim();

            if (cbEndDate.Checked)
            {
                m_currentUser.EndDate = this.dtpEndDate.Value;
            }
            else
            {
                m_currentUser.EndDate = null;
            }

            m_currentUser.IsGuest = this.cbIsGuest.Checked;

            return m_currentUser;
        }

        public void SetUser(User user)
        {
            m_currentUser = user;
            this.txtName.Text = user.Name;
            this.txtUserName.Text = user.UserName;
            this.txtPassword.Text = user.Password;

            if (user.Sex.Equals("男")) this.rbtnMan.Checked = true;
            else this.rbtnWomen.Checked = true;

            this.txtWork.Text = user.Work;
            this.txtContractNO.Text = user.ContractNO;
            this.txtEmail.Text = user.Email;

            if (user.EndDate.HasValue)
            {
                this.cbEndDate.Checked = true;
                this.dtpEndDate.Value = user.EndDate.Value;
            }
            else
            {
                this.cbEndDate.Checked = false;
            }
            
            this.cbIsGuest.Checked = user.IsGuest;
            this.cbIsNotGuest.Checked = !user.IsGuest;
        }

        public bool Vali()
        {
            if (String.IsNullOrEmpty(this.txtName.Text.Trim()))
            {
                Tool.ShowInformationBox("必须输入姓名");
                this.txtName.Focus();
                return false;
            }

            if (String.IsNullOrEmpty(this.txtUserName.Text.Trim()))
            {
                Tool.ShowInformationBox("必须输入用户名");
                this.txtUserName.Focus();
                return false;
            }

            if (String.IsNullOrEmpty(this.txtPassword.Text.Trim()))
            {
                Tool.ShowInformationBox("必须输入");
                this.txtPassword.Focus();
                return false;
            }

            return true;
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
